<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//echo dirname(dirname(dirname(dirname(__FILE__)))) . '/engine/start.php';
include_once(dirname(dirname(dirname(dirname(__FILE__)))) . '/engine/start.php');
//echo 0;
//
include_once(dirname(dirname(__FILE__)) . '/_init.php');
//
$user = get_user_by_username('greengar');
//echo 1;
login($user);
//
//echo 2;
$start = get_input('start');
//echo 3;
$end = get_input('end');
//echo 4;
$filename = "places";
//echo $filename;
$jsons = file($filename);

$mocks = array();
//echo 6;
$count = 0;

foreach ($jsons as $json) {
    
    if($count < $start) {
        $count++;
        continue;
    }
    
    if($count > $end) {
        break;
    }
    //echo $json;
    //echo '1';
    $json_place = json_decode($json, TRUE);
    if(is_array($json_place) && $json_place['name']) {
        $name = $json_place['name'];
        $lat = $json_place['lat'];
        $long = $json_place['long'];
        $mocks[$name] = array($name, '', $lat, $long);
    }
    
    $count++;
}

//die();

$count = $start;

foreach($mocks as $key => $value) {
    
    if($count < $start) {
        $count++;
        continue;
    }
    
    if($count > $end) {
        break;
    }
    
    $name = $key;
    $annotation = array(
        "category" => $value[0],
        "image_url" => $value[1]
    );
    $annotation = json_encode($annotation);
    $rating = 2;
    $interest = array(
        INTEREST_NAME => $name,
        INTEREST_ANNOTATION => $annotation,
        INTEREST_RATING_INTEREST_RATING => $rating);

    $ratingEntity = greengar_opinions_add_interest($interest, get_loggedin_userid());
    if($ratingEntity) {
        $rating = $ratingEntity->toArray();
        $interest = $rating['interest'];
        $interestId = $interest['guid'];
        $interestEntity = get_entity($interestId);
        $interestEntity->saveLocation($value[2], $value[3]);
        echo json_encode($ratingEntity->toArray()) . '<br>';
    } else {
        echo "Fail " . json_encode($interest) . '<br>';
    }
    
    $count++;
}

?>
